package com.ruoyi.web.controller.ydz;

import java.util.List;

import com.ruoyi.web.domain.News;
import com.ruoyi.web.domain.OldDrug;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.domain.OldNews;
import com.ruoyi.web.service.IOldNewsService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 新闻（旧）Controller
 *
 * @author drx
 * @date 2024-03-18
 */
@Controller
@RequestMapping({"/api/oldNews", "/ydz/oldNews"})
@Api(tags = "新闻（旧）")
public class OldNewsController extends BaseController {
    private String prefix = "ydz/oldNews";

    @Autowired
    private IOldNewsService oldNewsService;

    @RequiresPermissions("ydz:oldNews:view")
    @GetMapping()
    public String oldNews() {
        return prefix + "/oldNews";
    }

    /**
     * 查询新闻（旧）列表
     */
    @RequiresPermissions("ydz:oldNews:list")
    @PostMapping("/list")
    @ResponseBody
    @ApiOperation("查询新闻（旧）列表")
    public TableDataInfo list(OldNews oldNews) {
        startPage();
        List<OldNews> list = oldNewsService.selectOldNewsList(oldNews);
        return getDataTable(list);
    }

    /**
     * 查询新闻管理（旧）列表
     */
    @PostMapping("/selectNewsList")
    @ResponseBody
    @ApiOperation("查询新闻管理（新）列表")
    public TableDataInfo selectNewsList(@RequestBody OldNews oldNews) {
        startPage();
        List<OldNews> list = oldNewsService.selectOldNewsList(oldNews);
        return getDataTable(list);
    }

    /*
     * 增加浏览量
     * */
    @GetMapping("/updateNewsBrowseNum/{id}")
    @ResponseBody
    public void updateNewsBrowseNum(@PathVariable("id") Long id) {
        oldNewsService.updateNewsBrowseNum(id);
    }

    /*
     * 根据新闻id查询上一篇下一篇
     * */
    @GetMapping("/getUpandDown/{id}")
    @ResponseBody
    public AjaxResult getUpandDown(@PathVariable("id") Long id) {
        return oldNewsService.getUpandDown(id);
    }

    /**
     * 导出新闻（旧）列表
     */
    @RequiresPermissions("ydz:oldNews:export")
    @Log(title = "新闻（旧）", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    @ApiOperation("导出新闻（旧）列表")
    public AjaxResult export(OldNews oldNews) {
        List<OldNews> list = oldNewsService.selectOldNewsList(oldNews);
        ExcelUtil<OldNews> util = new ExcelUtil<OldNews>(OldNews.class);
        return util.exportExcel(list, "新闻（旧）数据");
    }

    /**
     * 新增新闻（旧）
     */
    @GetMapping("/add")
    @ApiOperation("新增新闻（旧）")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存新闻（旧）
     */
    @ApiOperation("新增保存新闻（旧）")
    @RequiresPermissions("ydz:oldNews:add")
    @Log(title = "新闻（旧）", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(OldNews oldNews) {
        return toAjax(oldNewsService.insertOldNews(oldNews));
    }

    /**
     * 修改新闻（旧）
     */
    @ApiOperation("修改新闻（旧）")
    @RequiresPermissions("ydz:oldNews:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        OldNews oldNews = oldNewsService.selectOldNewsById(id);
        mmap.put("oldNews", oldNews);
        return prefix + "/edit";
    }

    /**
     * 修改保存新闻（旧）
     */
    @ApiOperation("修改保存新闻（旧）")
    @RequiresPermissions("ydz:oldNews:edit")
    @Log(title = "新闻（旧）", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(OldNews oldNews) {
        return toAjax(oldNewsService.updateOldNews(oldNews));
    }

    /**
     * 删除新闻（旧）
     */
    @ApiOperation("删除新闻（旧）")
    @RequiresPermissions("ydz:oldNews:remove")
    @Log(title = "新闻（旧）", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(oldNewsService.deleteOldNewsByIds(ids));
    }


    /**
     * 修改保存新闻管理（旧）
     */
    @ApiOperation("修改保存新闻管理（旧）")
    @RequiresPermissions("ydz:oldNews:edit")
    @Log(title = "新闻管理（旧）", businessType = BusinessType.UPDATE)
    @PostMapping("/editState")
    @ResponseBody
    public AjaxResult editState(@RequestBody OldNews oldNews) {
        return toAjax(oldNewsService.updateOldNews(oldNews));
    }

    /**
     * 获取新闻详情-前端调用
     */
    @ApiOperation("获取新闻详情-前端调用")
    @GetMapping("/get/{id}")
    @ResponseBody
    public AjaxResult get(@PathVariable("id") Long id) {
        return oldNewsService.getOldNewsById(id);
    }

    /**
     * 获取药品页旧新闻视图
     */
    @GetMapping("/getDrugOldNewsView")
    @ResponseBody
    public AjaxResult getDrugOldNewsView() {
        return oldNewsService.getDrugOldNewsView();
    }
}
